<template>
  <view>
    <view class="search-box">
      <!-- 使用 uni-ui 提供的搜索组件 -->
      <uni-search-bar @input="input" :radius="100" cancelButton="none"></uni-search-bar>
    </view>
    <view class="search-list" v-if="searchResult.length !== 0">
      <view class="search-item" v-for="item in searchResult" :key="item.goods_id" @click="handleToGoodsDetail(item)">
        <view class="good-name">{{item.goods_name}}</view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clearHistory"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" v-for="(item, i) in histories" :key="i" @click="handleToList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:null,
        kw:'',
        searchResult:[],
        historyList:[]
      };
    },
    computed:{
      histories(){
        return [...this.historyList].reverse()
      }
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    },
    methods:{
      input(e){
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e.trim()
          this.getSearchResult()
        }, 500)
      },
      async getSearchResult(){
        if(this.kw.length === 0){
          this.searchResult = []
        }else {
          const res = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.kw})
          if (res.data.meta.status === 200) {
            this.searchResult = res.data.message
            this.saveHistoryList()
          }
        }
      },
      saveHistoryList(){
        this.historyList.push(this.kw)
        const set = new Set(this.historyList)
        set.delete(this.kw)
        set.add(this.kw)
        this.historyList = Array.from(set)
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      },
      handleToGoodsDetail(item){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
        })
      },
      clearHistory(){
        this.historyList = []
        uni.clearStorageSync('kw')
      },
      handleToList(val){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?query='+val
        })
      }
    }
  }
</script>

<style lang="scss">
.search_box{
  position: sticky;
  top: 0;
  z-index: 1;
}
.uni-searchbar{
  background-color: #c00000;
}
.search-list{
  .search-item{
    padding: 0 5px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .good-name{
      padding: 10px;
      font-size: 14px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }
  }
}
.history-box{
  padding: 0 10px;
  .history-title {
    height: 40px;
    display: flex;
    border-bottom: 1px solid #efefef;
    align-items: center;
    justify-content: space-between;
  }
  .history-list{
    display: flex;
    flex-wrap: wrap;
    .uni-tag{
      margin: 5px 10px 0 0;
    }
  }
}
</style>
